//
//  DoctorCommentViewController.m
//  安好
//
//  Created by lianchuang on 14-7-15.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "DoctorCommentViewController.h"
#import "HospitalCommentViewCell.h"

@interface DoctorCommentViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation DoctorCommentViewController
{
    NSMutableArray *commentsArray;
    NSInteger environment_score;
    NSInteger effect_score;
    NSInteger sercice_score;
    double score;
    NSInteger page;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    page=0;
    UILabel *titleLabel=[[UILabel alloc]init];
    UILabel *subLabel=[[UILabel alloc]init];
        titleLabel.frame=CGRectMake(60, 25, 200, 20);
        subLabel.frame=CGRectMake(60, 45, 200, 13);
    
    titleLabel.text=@"点评";
    subLabel.font=[UIFont systemFontOfSize:17];
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.textColor=[UIColor whiteColor];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    
    subLabel.text=self.subStr;
    subLabel.backgroundColor=[UIColor clearColor];
    subLabel.font=[UIFont systemFontOfSize:11];
    subLabel.textColor=[UIColor whiteColor];
    subLabel.textAlignment=NSTextAlignmentCenter;

    
    commentsArray=[[NSMutableArray alloc]init];
    
    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    self.tableview.allowsSelection=NO;
    [self.view addSubview:self.tableview];
    //点评
    self.encyCommentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.encyCommentButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.encyCommentButton setTitle:@"我要点评" forState:UIControlStateNormal];
    [self.encyCommentButton addTarget:self action:@selector(pushToCommentVC) forControlEvents:UIControlEventTouchUpInside];
    [self.encyCommentButton setBackgroundImage:[UIImage imageNamed:@"setting_logout.png"] forState:UIControlStateNormal];
    self.encyCommentButton.frame = CGRectMake(self.view.left + 16,self.view.frame.size.height - 50 ,IPHONE_WIDTH - 16*2, 45);
    [self.view addSubview:self.encyCommentButton];
    
    [self loadDoctorCommentInfo];
}
- (void)loadDoctorCommentInfo
{
    NSString * postURL = [NSString stringWithFormat:@"%@wiki/hospitalComment",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *select;
    if ([self.type isEqualToString:@"1"]) {
        select=@"hospitalid";
    }else if ([self.type isEqualToString:@"2"])
    {
        select=@"hospdeptid";
    }
    else if ([self.type isEqualToString:@"3"])
    {
        select=@"doctorid";
    }
    NSDictionary *paramers=@{
                             @"platform": kPlatform,
                             @"version":kVersion,
                             @"page":[NSString stringWithFormat:@"%ld",page+1],
                             @"type":self.type,
                             select:self.commendid,
                             };
    
    [manager POST:postURL parameters:paramers success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.encyCommentDic = responseObject ;
        if ([[self.encyCommentDic objectForKey:@"data"] count] == 0) {
        }else{
            NSArray *encyCArray = [[self.encyCommentDic objectForKey:@"data"] objectForKey:@"commentdata"];
            for(int index = 0;index < encyCArray.count; index++) {
                NSDictionary *dict = [encyCArray objectAtIndex:index];
                [commentsArray addObject:dict];
            }
            effect_score = [[[_encyCommentDic objectForKey:@"data"] objectForKey:@"effect_score"] intValue];
            environment_score = [[[_encyCommentDic objectForKey:@"data"] objectForKey:@"environment_score"] intValue];
            sercice_score = [[[_encyCommentDic objectForKey:@"data"] objectForKey:@"sercice_score"] intValue];
            score =[[[_encyCommentDic objectForKey:@"data"] objectForKey:@"score"] doubleValue];
        }
        self.tableview.hidden = NO;
        [self hiddenActionView];
        [self.tableview reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self shouLabel:@"暂无数据"];
    }];
}
- (void)pushToCommentVC
{
//    NSUserDefaults *user= [NSUserDefaults standardUserDefaults];
//    NSString * login = [user objectForKey:@"login"];
//    if ([login length]>0)
//    {
//        EncyCommentVC *encyCommentVC = [[EncyCommentVC alloc] init];
//        encyCommentVC.doctorid = self.doctorId;
//        encyCommentVC.delegate = self;
//        encyCommentVC.type = DOCTORTYPE;
//        encyCommentVC.subStr=self.subStr;
//        [self.navigationController pushViewController:encyCommentVC animated:YES];
//    }else
//    {
//        [self islogin];
//    }

}

#pragma mark TabelviewDatasource 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }
    else
    {
        return commentsArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        static NSString *indentifier=@"hospitalcell";
        UITableViewCell *encyHDetailCell=[tableView cellForRowAtIndexPath:indexPath];
        if (encyHDetailCell==nil) {
            encyHDetailCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        }
        //医院环境
        UIImageView *hospitalView = [[UIImageView alloc] initWithFrame:CGRectMake(15, SPACING + 2.5, 20, 20)];
        hospitalView.image = [UIImage imageNamed:@"医院环境.png"];
        
        UILabel *hospitalLab = [[UILabel alloc] initWithFrame:CGRectMake(hospitalView.right + 9, SPACING, 65, 25)];
        hospitalLab.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
        hospitalLab.text = [NSString stringWithFormat:@"【医院环境】：%ld",(long)environment_score];
        hospitalLab.text = [NSString stringWithFormat:@"医院环境:"];
//        hospitalLab.textColor = [self colorWithHexString:@"#333333"];
        for (int index = 0; index < 5 ; index++) {
            UIImageView *garstarView = [[UIImageView alloc] initWithFrame:CGRectMake(hospitalLab.right + index*25 , SPACING + 3, 19, 18)];
            garstarView.image = [UIImage imageNamed:@"点评2.png"];
            [encyHDetailCell addSubview:garstarView];
            
        }
        
        for (int index = 0; index<environment_score ; index++)
        {
            UIImageView *garstarView = [[UIImageView alloc] initWithFrame:CGRectMake(hospitalLab.right + index*25 , SPACING + 3, 19, 18)];
            garstarView.image = [UIImage imageNamed:@"点评1.png"];
            [encyHDetailCell addSubview:garstarView];
        }
        
        UILabel *sorceLab = [[UILabel alloc] initWithFrame:CGRectMake(hospitalLab.right + 152, SPACING, 24, 25)];
        sorceLab.text = @"评分";
        sorceLab.font = [UIFont systemFontOfSize:12];
//        sorceLab.textColor = [self colorWithHexString:@"#28aae4"];
        
        
        UILabel *sorceCLab = [[UILabel alloc] initWithFrame:CGRectMake(sorceLab.right-53, sorceLab.bottom, 80, 40)];
        sorceCLab.text = [NSString stringWithFormat:@"%.1f",score];
        sorceCLab.font = [UIFont systemFontOfSize:40];
        sorceCLab.textAlignment = NSTextAlignmentCenter;
//        sorceCLab.textColor = [self colorWithHexString:@"#ff9600"];
        
        //治疗效果
        UIImageView *cureView = [[UIImageView alloc] initWithFrame:CGRectMake(15, hospitalView.bottom + 5, 20, 20)];
        cureView.image = [UIImage imageNamed:@"治疗效果.png"];
        
        UILabel *cureLab = [[UILabel alloc] initWithFrame:CGRectMake(cureView.right + 9, hospitalLab.bottom, 65, 25)];
        cureLab.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
        cureLab.text = [NSString stringWithFormat:@"【治疗效果】：%ld",(long)effect_score];
        cureLab.text = [NSString stringWithFormat:@"治疗效果:"];
//        cureLab.textColor = [self colorWithHexString:@"#333333"];
        for (int index = 0; index < 5 ; index++) {
            UIImageView *garstarView = [[UIImageView alloc] initWithFrame:CGRectMake(cureLab.right + index*25 , hospitalLab.bottom + 3, 19, 18)];
            garstarView.image = [UIImage imageNamed:@"点评2.png"];
            [encyHDetailCell addSubview:garstarView];
            
        }
        for (int index = 0; index<effect_score; index++)
        {
            UIImageView *garstarView = [[UIImageView alloc] initWithFrame:CGRectMake(cureLab.right + index*25 , hospitalLab.bottom + 3, 19, 18)];
            garstarView.image = [UIImage imageNamed:@"点评1.png"];
            [encyHDetailCell addSubview:garstarView];
        }
        //服务态度
        UIImageView *sereveView = [[UIImageView alloc] initWithFrame:CGRectMake(15, cureView.bottom + 5, 20, 20)];
        sereveView.image = [UIImage imageNamed:@"服务态度.png"];
        
        UILabel *sereveLab = [[UILabel alloc] initWithFrame:CGRectMake(sereveView.right + 9,cureLab.bottom,65, 25)];
        sereveLab.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
        sereveLab.text = [NSString stringWithFormat:@"服务态度:"];
//        sereveLab.textColor = [self colorWithHexString:@"#333333"];
        for (int index = 0; index < 5 ; index++) {
            UIImageView *garstarView = [[UIImageView alloc] initWithFrame:CGRectMake(sereveLab.right + index*25 , cureLab.bottom + 3, 19, 18)];
            garstarView.image = [UIImage imageNamed:@"点评2.png"];
            [encyHDetailCell addSubview:garstarView];
            
        }
        for (int index = 0; index<sercice_score ; index++)
        {
            UIImageView *garstarView = [[UIImageView alloc] initWithFrame:CGRectMake(sereveLab.right + index*25 , cureLab.bottom + 3, 19, 18)];
            garstarView.image = [UIImage imageNamed:@"点评1.png"];
            [encyHDetailCell addSubview:garstarView];
        }
        encyHDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [encyHDetailCell addSubview:hospitalView];
        [encyHDetailCell addSubview:hospitalLab];
        [encyHDetailCell addSubview:sorceLab];
        [encyHDetailCell addSubview:sorceCLab];
        
        [encyHDetailCell addSubview:cureView];
        [encyHDetailCell addSubview:cureLab];
        
        [encyHDetailCell addSubview:sereveView];
        [encyHDetailCell addSubview:sereveLab];
        return encyHDetailCell;
    }
    else
    {
        static NSString *indentifier=@"cellw";
        HospitalCommentViewCell *encycommentCell=[tableView  dequeueReusableCellWithIdentifier:indentifier];
        if (encycommentCell==nil) {
            encycommentCell=[[[NSBundle mainBundle] loadNibNamed:@"HospitalCommentViewCell" owner:self options:nil] objectAtIndex:0];
        }
        NSDictionary *dic = [commentsArray objectAtIndex:indexPath.row];
        [encycommentCell.userImageview setImageWithURL:[NSURL URLWithString:[dic valueForKey:@"avatar"]] placeholderImage:[UIImage imageNamed:@"noavatar_big.png"]];
        encycommentCell.userName.text=[dic valueForKey:@"username"];
//        encycommentCell.userName.textColor=[self colorWithHexString:@"#4fc1e9"];
        
        encycommentCell.userComment.text=[dic valueForKey:@"comment"];
        encycommentCell.userComment.numberOfLines=0;
        CGSize size =CGSizeMake(215,0);
//        encycommentCell.userComment.textColor=[self colorWithHexString:@"#a0a0a0"];
        encycommentCell.userComment.font=[UIFont systemFontOfSize:14];
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:encycommentCell.userComment.font,NSFontAttributeName,nil];
        CGSize  actualsize;
        
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0){
            actualsize =[encycommentCell.userComment.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine  attributes:tdic context:nil].size;
        }else
        {
            CGSize size = CGSizeMake(215, 88888);
            encycommentCell.userComment.numberOfLines=0;
            encycommentCell.userComment.font=[UIFont systemFontOfSize:14];
            encycommentCell.userComment.lineBreakMode=NSLineBreakByCharWrapping;
            CGSize labelsize = [encycommentCell.userComment.text sizeWithFont:encycommentCell.userComment.font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
            actualsize=labelsize;
        }
        encycommentCell.userComment.frame=CGRectMake(75 ,37 , actualsize.width, actualsize.height);
        encycommentCell.dateLine.text=TextString([dic valueForKey:@"dateline"]);
        return encycommentCell;
    }
}

#pragma mark TableviewDelegate //代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 95;
    }else
    {
        
        HospitalCommentViewCell *cell=(HospitalCommentViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        
        CGFloat f=cell.userComment.frame.size.height<22?72:72+cell.userComment.frame.size.height-20;
        return f;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
//    view.backgroundColor=[self colorWithHexString:@"#ecf4f6"];
    return view;
}
#pragma mark 评论成功后代理
-(void)appraiseFinishWithText:(NSString *)text andHosBtnCount:(int)hosBtnCount andCurBtnCount:(int)curBtnCOunt andServiceBtnCount:(int)serviceBtnCount
{
    page = 0;
    [commentsArray removeAllObjects];
    [self loadDoctorCommentInfo];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
